package com.zcp.pcsuitall.business;

import java.io.IOException;
import java.io.StringWriter;
import java.nio.channels.SocketChannel;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.LinkedList;

import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.util.Xml;

import com.zcp.pcsuitall.Action;
import com.zcp.pcsuitall.BusinessControl;
import com.zcp.pcsuitall.IDealWithMessage;
import com.zcp.pcsuitall.XmlDataDealWith;

public class CalendarManager implements IDealWithMessage {
	private SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private Date dateTime = new Date(0l);
	private Timestamp ts = new Timestamp(0l);
	
	private static String calanderURL = "";
	private static String calanderEventURL = "";
	private static String calanderRemiderURL = "";


	//为了兼容不同版本的日历,2以后url发生改变
	static{
	if(Integer.parseInt(Build.VERSION.SDK) >= 8){
		calanderURL = "content://com.android.calendar/calendars";
		calanderEventURL = "content://com.android.calendar/events";
		calanderRemiderURL = "content://com.android.calendar/reminders"; 
	
		}else{
		calanderURL = "content://calendar/calendars";
		calanderEventURL = "content://calendar/events";
		calanderRemiderURL = "content://calendar/reminders";
		}
	}
	

	@Override
	public boolean dealWith(SocketChannel sc, Action action, Context context) {
		boolean success = false;
		if (action != null) {
			String op = action.getName();
			if (op.equals("LoadCalendar")) {
				success = dealWithLoad(sc, action, context);
			}
		}
		return success;
	}

	LinkedList<SmsInfoObject> smsList = null;

	/**
	 * 处理查询方法。
	 */
	private boolean dealWithLoad(SocketChannel sc, Action action,
			Context context) {

		try {
			System.out.println("//action//"+action.getName());
			String result = LoadSMS(context, action);// 以字符形式的XML输出
			System.out.println("////"+result);
			BusinessControl.returnToClient(sc, result, context);
			smsList.clear();
			return true;
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
			BusinessControl.returnToClient(
					sc,
					XmlDataDealWith.getSystemErrorString(action.getName(),
							e.toString()), context);
			return false;
		}
	}

	LinkedList<SmsInfoObject> resultList = new LinkedList<SmsInfoObject>();

	/**
	 * 查询。
	 * @param context
	 * @return
	 */
	private String LoadSMS(Context context, Action action) {
		resultList.clear();
		Cursor c = null;
		Cursor userCursor = null;

		int id = 0;
		String title = "";
		String name = "";

		StringWriter writer = new StringWriter();
		XmlSerializer serializer = Xml.newSerializer();
		try {
			serializer.setOutput(writer);
			XmlDataDealWith.createPre(action, serializer);
			Uri uri = Uri.parse(calanderEventURL);
			c = context.getContentResolver().query(uri,null, null, null, null);
			userCursor = context.getContentResolver().query(Uri.parse(calanderURL),null, null, null, null);

			for(int i=0;i<c.getColumnCount();i++){
				System.out.println(c.getColumnName(i));
			}
			boolean have = c.moveToFirst();
			System.out.println(have);
			if (have) {
				do {
					serializer.startTag(SPACE, "title");
					serializer.text(c.getString(c.getColumnIndex("title")));
					serializer.endTag(SPACE, "title");
					
				} while (c.moveToNext());
			}
			boolean have1 = userCursor.moveToFirst();
			System.out.println(have1);
			if (have1) {
				do {
					String userName = userCursor.getString(userCursor.getColumnIndex("name"));
					serializer.startTag(SPACE, NAME);
					serializer.text(userName);
					serializer.endTag(SPACE, NAME);
				} while (c.moveToNext());
			}
			
			serializer.endTag(SPACE, DATA);
			serializer.endTag(SPACE, ROOT);
			serializer.endDocument();
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return XmlDataDealWith.getSystemErrorString(action.getName(),
					e.toString());
		} finally {
			serializer = null;
			try {
				c.close();
				userCursor.close();
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			writer = null;
		}
	}

	public static final int INCOMING_TYPE = 1;
	public static final int OUTGOING_TYPE = 2;
	public static final int MISSED_TYPE = 3;

	private String NUMBER = "number";
	private String DATE = "date";
	private String DURATION = "duration";
	private String NEW = "new";
	private String TYPE = "type";
	private String NAME = "name";

	private String SPACE = "";

	private String ITEM = "item";

	private String DATA = "data";

	private String ROOT = "root";

	private String NOSETTING = "notSetting";
	private static String TAG = "SmsManager";
}
